#-*- coding: utf-8 -*-
import moviepy.editor as mp

def add_watermark_by_raw_file(filename, output_filename = None):
    video = mp.VideoFileClip(filename)
    logo = (mp.ImageClip("./logo/logo.png")
          .set_duration(video.duration)  # 水印持续时间
          .resize(height=130)  # 水印的高度，会等比缩放
          .margin(right=8, top=8, opacity=1)  # 水印边距和透明度
          .set_pos(("right","top")))  # 水印的位置
    # 合并两个视频    
    result = mp.CompositeVideoClip([video, logo])
    if output_filename != None:
        result.write_videofile(output_filename, codec="libx264", bitrate="10000000")
    
    return result

def add_watermark(video):
    logo = (mp.ImageClip("./logo/logo.png")
          .set_duration(video.duration)  # 水印持续时间
          .resize(height=int(video.size[1] * 0.15))  # 水印的高度，会等比缩放
          .margin(right=8, top=8, opacity=1)  # 水印边距和透明度
          .set_pos(("right","top")))  # 水印的位置
    # 合并两个视频    
    return mp.CompositeVideoClip([video, logo])

if __name__ == "__main__":
    add_watermark("./videos/test.mp4", "./videos/test-add.mp4")